Универсальный робот
Автор
Hidden goal
Now let us look at other possible tasks for our robots.
First, let it forget the map and let us hide a goal at a random point. Now the robot should stop once it reaches the goal.
In [ ]:
# Initialize memory
init_memory!(robot, start_x, start_y, dir_x, dir_y, size(room)[1], size(room)[2])
# Set goal: explore (with sensors) the whole room
replace!(robot.map, unknown => goal_check);
# Create a random goal position
while true
global goal_pos = Cind(rand(2:size(room,1)-1),rand(2:size(room,2)-1))
if room[goal_pos] == empty
room[goal_pos] = goal
break
end
end
println(goal_pos)
In [ ]:
run_robot!(robot, room)
Out[0]:
Known goal
Let us repeat the process, but now we tell the robot goal location in advance.
In [ ]:
# Initialize memory
init_memory!(robot, start_x, start_y, dir_x, dir_y, size(room)[1], size(room)[2])
# Set goal location into memory
robot.destination = goal_pos
robot.map[goal_pos] = 2;
In [ ]:
run_robot!(robot, room)
Out[0]:
When destination position flickers, it means robot encountered a wall on the assumed path and thus constructs a new path.
Exit search
Finally, let it search for an exit in the room. We put the goal randomly at one of the walls and tell robot it is somewhere on the border.
In [ ]:
# Initialize memory
init_memory!(robot, start_x, start_y, dir_x, dir_y, size(room)[1], size(room)[2])
# Set goal: find exit inside the room walls
replace!(robot.map, wall => goal_check)
# Remove previous goal
room[goal_pos] = 0
# Put goal at one of the walls
x = rand(1:2*size(room,1)+2*size(room,2)-4)
# Can run indefinitely in the unprobable case when all room walls are again surrounded by walls
# (i.e. all have depth 2 instead of 1)
while true
if (x <= size(room,1)-2)
if room[x+1,2] != wall
goal_pos = Cind(x+1,1)
break
end
else
x -= size(room,1)-2
if (x <= size(room,1)-2)
if room[x+1,size(room,2)-1] != wall
goal_pos = Cind(x+1,size(room,2))
break
end
else
x -= size(room,1)-2
if (x <= size(room,2)-2)
if room[2,x+1] != wall
goal_pos = Cind(1,x+1)
break
end
else
x -= size(room,2)-2
if room[size(room,1)-1,x+1] != wall
goal_pos = Cind(size(room,1),x+1)
break
end
end
end
end
end
room[goal_pos] = goal
println(goal_pos)
In [ ]:
run_robot!(robot, room)
Out[0]:


